package main

import "fmt"

func main() {
	candy([]int{1, 2, 3, 1})
}

func candy(ratings []int) int {
	m := make([]int, len(ratings))
	for i := 0; i < len(ratings); i++ {
		m[i] = 1
	}

	for i := 1; i < len(ratings); i++ {
		if ratings[i] > ratings[i-1] {
			m[i] = max(m[i], m[i-1]+1)
		}
	}

	for i := len(ratings) - 2; i >= 0; i-- {
		if ratings[i] > ratings[i+1] {
			m[i] = max(m[i], m[i+1]+1)
		}
	}
	fmt.Println(m)

	var o int
	for _, v := range m {
		o += v
	}
	return o
}

func candy2(ratings []int) int {
	m := make([]int, len(ratings))
	for i := 0; i < len(ratings); i++ {
		m[i] = 1
	}

	for i := 0; i < len(ratings)-1; i++ {
		if ratings[i] > ratings[i+1] {
			m[i] = max(m[i], m[i-1]+1)
		}
	}

	for i := len(ratings) - 2; i >= 0; i-- {
		if ratings[i] > ratings[i+1] {
			m[i] = max(m[i], m[i+1]+1)
		}
	}
	fmt.Println(m)

	var o int
	for _, v := range m {
		o += v
	}
	return o
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
